fix(pg): throw on invalid Date instead of serializing NaN string#3665
Closed
terminalchai wants to merge 1 commit intobrianc:masterfrom
Closed
fix(pg): throw on invalid Date instead of serializing NaN string#3665terminalchai wants to merge 1 commit intobrianc:masterfrom
terminalchai wants to merge 1 commit intobrianc:masterfrom
Conversation
new Date(undefined) and new Date('invalid') produce an invalid Date
object whose getTime() returns NaN. Previously prepareValue() would
pass such a date straight through to dateToString() / dateToStringUTC()
which format each NaN component with padStart(), producing the
meaningless string '0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN' that Postgres
cannot parse.
Add an isNaN(val.getTime()) guard in the isDate branch of prepareValue()
and throw an informative error immediately, so callers discover the bug
at the JS level rather than receiving a cryptic Postgres error.
Fixes brianc#3318
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
new Date(undefined)andnew Date('not a date')produce an invalidDateobject whose.getTime()returnsNaN. PreviouslyprepareValue()would pass such a date straight through todateToString()/dateToStringUTC(), which format eachNaNtime component withpadStart(), producing the meaningless string:Postgres cannot parse that and responds with a cryptic server-side error. The bug is especially hard to track down because the garbled string originates silently in JavaScript — the user has no indication that an invalid
Datewas passed in the first place.Fixes #3318.
Fix
Add an
isNaN(val.getTime())guard in theisDatebranch ofprepareValue()and throw an informative error immediately:The error surfaces at the JavaScript call site, making the root cause immediately obvious.
Tests
Added two unit tests in
packages/pg/test/unit/utils-tests.js:new Date(undefined)→ throws/date is invalid/new Date('not a date')→ throws/date is invalid/